<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="gv-sub-content">
  <div class="gv-forms gv-forms-fluid" layout="column">
    <div class="gv-form" ng-show="!isLoading && $ctrl.applications.length > 0">
      <div class="apis-header-filter">
        <form flex layout="row" layout-align="center center" name="formSearch" autocomplete="off"
              ng-submit="$ctrl.loadMore(query.order, searchApplications, false)">
          <input class="form-control apis-filter" type="text" style="max-width: 960px"
                 ng-model="searchApplications" placeholder="Search Applications" autofocus/>
          <md-button class="md-primary gv-search-button" type="submit" aria-label="Search">
            <ng-md-icon icon="search"></ng-md-icon>
          </md-button>
        </form>
      </div>
      <div class="gv-form-content" id="applications-container"
           style="height: calc(100vh - 200px); overflow-y: auto">
        <div layout="row" layout-wrap class="inset" infinite-scroll="$ctrl.loadMore(query.order, searchApplications, true)"
             infinite-scroll-distance="1" infinite-scroll-container="'#applications-container'">
          <md-table-container style="width: 100%">
            <table md-table ng-model="$ctrl.selectedApplications" ng-init="query.order = 'name'">
              <thead md-head md-order="query.order">
              <tr md-row>
                <th md-column width="1%"></th>
                <th md-column md-order-by="name" ng-click="$ctrl.loadMore(query.order, searchApplications, false)"><span>Name</span></th>
                <th md-column width="1%" md-order-by="type" ng-click="$ctrl.loadMore(query.order, searchApplications, false)">Type</th>
                <th md-column width="1%" md-order-by="owner.displayName" ng-click="$ctrl.loadMore(query.order, searchApplications, false)">Owner</th>
              </tr>
              </thead>
              <tbody md-body>
              <tr md-row ng-repeat="application in $ctrl.applicationsToDisplay track by application.id">
                <td md-cell nowrap>
                  <gravitee-identity-picture 
                    image-id="application.id"
                    image-url="::application.picture_url"
                    image-name="application.name + '  ' + application.type + '  ' + application.owner.displayName"
                    image-width="40"
                  ></gravitee-identity-picture>
                </td>
                <td md-cell>
                  <a ui-sref="management.applications.application.general({applicationId: application.id})">
                    {{application.name}}
                  </a>
                </td>
                <td md-cell nowrap>{{application.type | lowercase}}</td>
                <td md-cell nowrap>
              <span>
                {{application.owner.displayName}}
                <md-tooltip>
                  <span ng-if="application.owner.email">{{::application.owner.email}}</span>
                </md-tooltip>
              </span>
                </td>
              </tr>
              </tbody>
            </table>
          </md-table-container>
        </div>
      </div>
    </div>

<gravitee-empty-state ng-if="$ctrl.applications.length == 0"
                      icon="list"
                      model="Application"
                      message="There is no application (yet)."
                      sub-message="{{$ctrl.subMessage}}"
                      create-mode="true"></gravitee-empty-state>

<div permission permission-only="'environment-application-c'" ng-style="$ctrl.applications.length == 0 && {'text-align':'center'}">
  <md-button ng-class="{'md-fab-bottom-right': $ctrl.applications.length > 0}" class="md-fab"
             aria-label="Create New Application" ui-sref="management.applications.create">
    <ng-md-icon icon="add"></ng-md-icon>
  </md-button>
</div>
